package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.localbean.GoodsData;
import com.wgts.wgts.mvp.model.IMyOrderFragmentModel;
import com.wgts.wgts.mvp.presenter.IMyOrderFragmentPersenter;
import com.wgts.wgts.mvp.view.IMyOrderFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.MyOrderListEntity;

import java.util.List;

public class MyOrderFragmentPersenter implements IMyOrderFragmentPersenter {

    private IMyOrderFragmentView view;

    private IMyOrderFragmentModel model;

    private int pageIndex = 1;
    private int pageSize = 20;

    public MyOrderFragmentPersenter(IMyOrderFragmentView view, IMyOrderFragmentModel model) {
        this.view = view;
        this.model = model;

    }


    @Override
    public void initList(String orderState) {
        pageIndex = 1;
        model.initList(orderState, pageIndex, pageSize, new HttpResultSubscriber<List<MyOrderListEntity>>() {
            @Override
            public void onSuccess(List<MyOrderListEntity> myOrderListEntity) {
                if (myOrderListEntity.size() < pageSize){
                    view.myOrderList(myOrderListEntity, true);
                }else {
                    view.myOrderList(myOrderListEntity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void cancelOrder(String orderId, final Integer position) {
        model.cancelOrder(orderId, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.againOrderList();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void takeGoods(String orderId) {
        model.takeGoods(orderId, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.againOrderList();
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void addMore(String orderState) {
        model.initList(orderState, pageIndex, pageSize, new HttpResultSubscriber<List<MyOrderListEntity>>() {
            @Override
            public void onSuccess(List<MyOrderListEntity> myOrderListEntity) {
                if (myOrderListEntity.size() < pageSize){
                    view.loadMore(myOrderListEntity, true);
                }else {
                    view.loadMore(myOrderListEntity, false);
                    pageIndex++;
                }

            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void goPay(String orderId, String needPayPrice, String orderType, String commissionProfit, String orderCode) {
        view.goPay(orderId, needPayPrice, orderType, commissionProfit, orderCode);
    }

    @Override
    public void goEvaluate(String orderId) {
        view.goEvaluate(orderId);
    }

    @Override
    public void applyForAfterSell(String orderId, String orderCode, GoodsData data) {
        view.applyForAfterSell(orderId, orderCode, data);
    }
}
